﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.APP.Classes;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using Telerik.Web.UI;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_NonWorkingDaysEdit : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private INonWorkingDayBLL nwdBLL = new NonWorkingDayBLL();
        private Main main = new Main();

        public PS_NonWorkingDaysEdit()
        {
            DataItem = null;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblNwdSeq.Text = DataBinder.Eval(DataItem, "Sequence").ToString();
                    lblBgYear.Text = DataBinder.Eval(DataItem, "BudgetYear").ToString();
                    txtNwdDate.SetValue(DataBinder.Eval(DataItem, "Date"));
                    txtNwdName.Text = Convert.ToString(DataBinder.Eval(DataItem, "Name"));
                    txtRemarks.Text = DataBinder.Eval(DataItem, "Remarks").ToString();
                }
                else
                {
                    lblBgYear.Text = SelectedBgYear.ToString();
                }

                DateTime firstDateOfBgYear = new DateTime(SelectedBgYear - 543, 10, 1);
                DateTime lastDateOfBgYear = new DateTime(SelectedBgYear - 542, 9, 30);
                lblMessageDateRangeNotice.Text = "กรุณาเลือกวันในช่วงระหว่าง " +
                                 firstDateOfBgYear.ToString("dd/MM/yyyy", new CultureInfo("th-TH")) +
                                 " - " +
                                 lastDateOfBgYear.ToString("dd/MM/yyyy", new CultureInfo("th-TH"));

                if (!txtNwdDate.GetDateNullableValue().HasValue || txtNwdDate.GetDateValue() < firstDateOfBgYear || txtNwdDate.GetDateValue() > lastDateOfBgYear)
                {
                    txtNwdDate.SetValue(firstDateOfBgYear);
                }
            }
        }


        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            if (!Validate()) return;

            nwdBLL.Insert(Int32.Parse(lblBgYear.Text)
                , DateTime.Parse(txtNwdDate.GetStringValue())
                , txtNwdName.Text.Trim()
                , txtRemarks.Text.Trim()
                , ssMng.EmpSeqI);
            main.MessageAlert(Page, "บันทึกการสร้างวันหยุดใหม่เรียบร้อยแล้ว");
        }


        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!Validate()) return;

            nwdBLL.Update(Int32.Parse(lblNwdSeq.Text)
                , Int32.Parse(lblBgYear.Text)
                , DateTime.Parse(txtNwdDate.GetStringValue())
                , txtNwdName.Text.Trim()
                , txtRemarks.Text.Trim()
                , ssMng.EmpSeqI);
            main.MessageAlert(Page, "บันทึกการแก้ไขวันหยุดใหม่เรียบร้อยแล้ว");
        }

        private bool Validate()
        {
            if (string.IsNullOrEmpty((txtNwdName.Text ?? "").Trim()))
            {
                main.MessageAlert(Page, "กรุณากรอกชื่อวันหยุด");
                return false;
            }

            if (txtNwdDate.GetStringValue().Length <= 0)
            {
                main.MessageAlert(Page, "กรุณาเลือกวัน");
                return false;
            }

            DateTime firstDateOfBgYear = new DateTime(SelectedBgYear - 543, 10, 1);
            DateTime lastDateOfBgYear = new DateTime(SelectedBgYear - 542, 9, 30);
            if (txtNwdDate.GetDateValue() < firstDateOfBgYear || txtNwdDate.GetDateValue() > lastDateOfBgYear)
            {
                string message = "สำหรับปีงบประมาณ " + SelectedBgYear + ", กรุณาเลือกวันในช่วงระหว่าง " +
                                 firstDateOfBgYear.ToString("dd/MM/yyyy", new CultureInfo("th-TH")) +
                                 " - " +
                                 lastDateOfBgYear.ToString("dd/MM/yyyy", new CultureInfo("th-TH"));
                main.MessageAlert(Page, message); 
                return false;
            }

            if (nwdBLL.GetAllNonWorkingDays(SelectedBgYear.ToString())
                .Any(o => o.RecordStatus != "D" && o.Date.HasValue && o.Date.Value.Date == txtNwdDate.GetDateValue()))
            {
                string message = "ท่านได้สร้างวันหยุดวันนี้ไว้แล้ว";
                main.MessageAlert(Page, message); 
                return false;
            }
            return true;
        }

        public object DataItem { get; set; }

        private int SelectedBgYear
        {
            get
            {

                if (string.IsNullOrEmpty(Convert.ToString(Request["BgYear"])))
                {
                    return Convert.ToInt32(DateTime.Now.AddYears(DateTime.Now.Month < 10 ? -1 : 0).ToString("yyyy", new CultureInfo("th-TH")));
                }
                else
                {
                    return Convert.ToInt32(Request["BgYear"]);
                }
            }
        }

    }
}